package com.klondike.common.io;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import java.io.IOException;
import java.io.Reader;
import java.util.Properties;

import org.junit.Test;

public class TheResourceUtils {

    @Test
    public void canLoadResourceViaURL() throws Exception {
        String content = ResourceUtils.getResourceAsString("com/klondike/common/io/TheIOUtils.txt");
        assertEquals("Sample Test Resource.", content);
    }

    @Test
    public void returnNullAsContentForNonExistUrl() throws Exception {
        String content = ResourceUtils.getResourceAsString("_not_exits");
        assertEquals(null, content);
    }

    @Test
    public void canLoadResourceViaContentClass() throws Exception {
        String content = ResourceUtils.getResourceAsString(TheResourceUtils.class, "TheIOUtils.txt");
        assertEquals("Sample Test Resource.", content);
    }
    @Test
    public void returnReaderForGivenResourcePath() throws Exception {
        Reader reader = ResourceUtils.getResourceAsReader(TheResourceUtils.class, "TheIOUtils.txt");

        assertEquals("Sample Test Resource.", IOUtils.toString(reader));
    }

    @Test
    public void canLoadConfigByResourcePath() throws Exception {
        Properties properties = ResourceUtils.getConfigurationByUrl("com/klondike/common/io/SampleConfig.properties");

        assertEquals(properties.size(), 8);
    }

    @Test
    public void canReturnNullForNonExistsResource() throws Exception {
        Reader reader = ResourceUtils.getResourceAsReader(TheResourceUtils.class, "non_exists", null, true);

        assertNull(reader);
    }

    @Test(expected = IOException.class)
    public void canThrowExceptionForNonExistsResource() throws Exception {
        Reader reader = ResourceUtils.getResourceAsReader(TheResourceUtils.class, "non_exists", null, false);

        assertNull(reader);
    }
    

}
